package com.frxuexi.shop.viewmodel

import com.frxuexi.lib_base.http.ShopApiService
import com.frxuexi.lib_base.mvi.BaseViewModel
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.room.ShopDao
import com.frxuexi.shop.intent.ShopIntent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

@HiltViewModel
class ShopViewModel @Inject constructor(val shopApiService: ShopApiService,val shopDao: ShopDao):BaseViewModel<ShopIntent>() {
    override fun handlerIntent(it: ShopIntent) {
        when(it){
            is ShopIntent.getHomeBean->httpRequest(shopApiService.getHomeBean())
            is ShopIntent.getShops->httpRequest(shopApiService.getShops(it.type,it.page,it.limit),RequestType.SHOPS)
            is ShopIntent.getShopById->httpRequest(shopApiService.getShopById(it.id,it.type))
            is ShopIntent.addCart->httpRequest(shopApiService.addCart(toBody(it.map)),RequestType.ADD)
            is ShopIntent.addLove->roomRequest(flow { emit(shopDao.insertShop(it.shopBean)) },RequestType.LOVE)
            is ShopIntent.isLove->roomRequest(flow { emit(shopDao.getIsLove(it.id)) },RequestType.ISLOVE)
            is ShopIntent.deleteLove->roomRequest(flow { emit(shopDao.deleteLove(it.shopBean)) },RequestType.DELETE)
            is ShopIntent.getReplys->httpRequest(shopApiService.getRepalys(it.id,it.limit,it.page,it.type),RequestType.REPLY)

            else -> {}
        }
    }
}